// API接口测试脚本
const axios = require('axios');

const BASE_URL = 'http://localhost:3001';

async function testAPI() {
    console.log('🧪 开始测试API接口...\n');

    try {
        // 测试1: 新的 :shop-:id 格式
        console.log('📋 测试1: 新的 :shop-:id 格式接口');
        console.log('GET /api/search-order/shop1-123456');
        
        try {
            const response1 = await axios.get(`${BASE_URL}/api/search-order/shop1-123456`);
            console.log('✅ 响应:', response1.data);
            
            // 检查返回格式
            if (response1.data.payment === -1) {
                console.log('📝 说明: 未找到订单数据');
            } else if (response1.data.payment === -2) {
                console.log('📝 说明: 查询失败');
            } else {
                console.log('📝 说明: 查询成功，返回订单金额和详情');
            }
        } catch (error) {
            console.log('❌ 错误:', error.response?.data || error.message);
        }
        console.log('');

        // 测试2: 兼容性接口
        console.log('📋 测试2: 兼容性接口');
        console.log('GET /api/search-order/123456');
        
        try {
            const response2 = await axios.get(`${BASE_URL}/api/search-order/123456`);
            console.log('✅ 响应:', response2.data);
            
            // 检查返回格式
            if (response2.data.payment === -1) {
                console.log('📝 说明: 未找到订单数据');
            } else if (response2.data.payment === -2) {
                console.log('📝 说明: 查询失败');
            } else {
                console.log('📝 说明: 查询成功，返回订单金额和详情');
            }
        } catch (error) {
            console.log('❌ 错误:', error.response?.data || error.message);
        }
        console.log('');



        console.log('\n📋 返回格式说明:');
        console.log('  payment: -1  -> 未找到订单数据');
        console.log('  payment: -2  -> 查询失败');
        console.log('  payment: >0  -> 订单金额，同时返回订单详情');
        console.log('');
        console.log('📋 完整返回格式示例:');
        console.log('  成功: { payment: 5999.00, order_number: "123456", upload_time: "2024-01-01 12:00:00" }');
        console.log('  未找到: { payment: -1 }');
        console.log('  失败: { payment: -2 }');

    } catch (error) {
        console.error('❌ 测试过程中发生错误:', error.message);
    }
}

// 运行测试
if (require.main === module) {
    testAPI();
}

module.exports = { testAPI }; 